package crawler.cnki.task;

import crawler.cnki.selenium.SeleniumDownloader;
import crawler.cnki.selenium.WebDriverPool;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.scheduler.RedisScheduler;
import us.codecraft.webmagic.selector.Html;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Component
public class PatentProcessor implements PageProcessor {
    @Autowired
    private DataPipeline dataPipeline;
    @Autowired
    private JedisPool jedisPool;

    private String patentHead[] = {"http://s.wanfangdata.com.cn/patent?q=%E5%9F%BA%E4%BA%8E",
            "http://s.wanfangdata.com.cn/patent?q=%E7%A0%94%E7%A9%B6",
            "http://s.wanfangdata.com.cn/patent?q=%E8%AE%A1%E7%AE%97",
            "http://s.wanfangdata.com.cn/patent?q=%E8%87%AA%E5%8A%A8",
            "http://s.wanfangdata.com.cn/patent?q=%E6%8A%80%E6%9C%AF",
            "http://s.wanfangdata.com.cn/patent?q=%E7%8E%AF%E5%A2%83",
            "http://s.wanfangdata.com.cn/patent?q=%E8%83%BD%E6%BA%90"};
    String root = "http://s.wanfangdata.com.cn/patent?q=";


    @Scheduled(fixedDelay = 1000 * 1000)
    public void PatentStarter() {
        Spider.create(new PatentProcessor())
                .setUUID("Patent")
                .setScheduler(new RedisScheduler(jedisPool))
                .addUrl(patentHead)
                .setDownloader(new SeleniumDownloader(0, PoolUtil.getWebDriverPool(), new DataAction()))
                //.setScheduler(new QueueScheduler().setDuplicateRemover(new BloomFilterDuplicateRemover(100000)))
                .addPipeline(dataPipeline)
                .thread(1)
                .run();

    }

    @Override
    public void process(Page page) {
        Html html = page.getHtml();
        String currentUrl = page.getUrl().toString();
        //如果是学者门户，将保存信息到学者
        if (currentUrl.contains("trend.") && currentUrl.contains("scholarName")) {
            page.putField("itemType", 4);
            Document scholarInfo = Jsoup.parse(html.css("div.scholar-infor").toString());
            //学者姓名
            String name = scholarInfo.select("h3.scholar-name-show").text();
            page.putField("name", name);
            //学者机构
            String organization = scholarInfo.select("h3.unit-name").text();
            page.putField("organization", organization);

        }
        //如果是专利页面
        else if (currentUrl.contains("d.wanfangdata") && currentUrl.contains("patent/")) {

            page.putField("itemType", 2);
            Document detailInfo = Jsoup.parse(html.css("div.detailInfo").toString());

            //获取标题
            String title = detailInfo.select("div.detailTitle span.detailTitleCN").text();
            page.putField("title", title);

            try {
                page.addTargetRequest(root+java.net.URLEncoder.encode(title.substring(title.length()-2),"utf-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            //摘要
            Elements summarys = detailInfo.select("div.summary");
            if (!summarys.isEmpty()) {
                String summary = summarys.first().ownText();
                page.putField("summary", summary);
            }

            //获取类型
            String type = detailInfo.select("div.patentType div.itemUrl").text();
            page.putField("type", type);

            //获取专利号
            String patentID = detailInfo.select("div.patentCode div.itemUrl").text();
            page.putField("patentID", patentID);

            //获取公开号
            String publishID = detailInfo.select("div.publicationNo div.itemUrl").text();
            page.putField("publishID", publishID);

            //获取申请日期和公开日期
            Elements dates = detailInfo.select("div.applicationDate div.itemUrl");
            if (dates.size() > 1) {
                String applyDate = dates.get(0).text();
                page.putField("applyDate", applyDate);
                String publicDate = dates.get(1).text();
                page.putField("publicDate", publicDate);
            }

            //获取主分类号
            String mainTypeNumber = detailInfo.select("div.mainClassCode div.itemUrl").text();
            page.putField("mainTypeNumber", mainTypeNumber);

            //获取分类号
            String typeNumber = detailInfo.select("div.classify div.itemUrl").text();
            page.putField("typeNumber", typeNumber);

            //获取发明/设计人
            Elements applicants = detailInfo.select("div.applicant div.itemUrl");
            if (dates.size() > 1) {
                String owner = applicants.get(0).text();
                page.putField("owner", owner);
                String designer = applicants.get(1).text().replaceAll(" ", ",");
                page.putField("designer", designer);
            }

            //获取主申请人地址
            String address = detailInfo.select("div.applicantAddress div.itemUrl").text();
            page.putField("address", address);

            //获取专利代理机构
            String agency = detailInfo.select("div.agency div.itemUrl").text();
            page.putField("agency", agency);

            //获取代理人
            String agent = detailInfo.select("div.agent div.itemUrl").text();
            page.putField("agent", agent);

            //获取国别省市代码
            String code = detailInfo.select("div.applicantArea div.itemUrl").text();
            page.putField("code", code);

            //获取主权项
            String description = detailInfo.select("div.signoryItem div.itemUrl").text();
            page.putField("description", description);

            //获取法律状态
            String status = detailInfo.select("div.legalStatus div.itemUrl").text();
            page.putField("status", status);

//            List<String> list = html.css("div.author div.itemUrl").links().all();
//            page.addTargetRequests(list);
        }
        //如果是搜索页面
        else{
            List<String> links= html.css("div.result-list-container div.title-area").links().all();
            if(links.size()==0) return;
            page.addTargetRequests(links);
            if(!currentUrl.contains("&p=")){
                page.addTargetRequest(currentUrl+"&p=2");
            }else{
                String s = currentUrl;
                int i = Integer.parseInt(s.substring(s.lastIndexOf("=")+1));
                s = s.substring(0,s.lastIndexOf("=")+1)+(i+1);
                page.addTargetRequest(s);
            }
        }
    }

    private Site site = Site.me()
            .setCharset("utf8")
            .setTimeOut(10 * 1000)
            .setRetrySleepTime(3000)
            .setRetryTimes(3);

    @Override
    public Site getSite() {
        return site;
    }
}
